//app.js
var template = require('template/template.js');
let host = "https://testappapi.gongcing.com/"  //本地测试
// let host ="https://appapi.gongcing.com/"   //线上
App({
  onLaunch: function() {
    // 登录
    let token = wx.getStorageSync("token")
    let userInfo=wx.getStorageSync("userInfo")
    if (userInfo){
      userInfo = JSON.parse(userInfo)
    }
    if (token) {
      if(userInfo.idcard==""){
        wx.reLaunch({
          url: './pages/selectType/selectType'
        })
      }else{
        if (userInfo.doctor_id>0){

          if (userInfo.doctor_status == 0) {
            return wx.showToast({
              title: '信息待审核',
              icon: 'none',
              success: function () {
                wx.removeStorageSync("token")
                wx.removeStorageSync("userInfo")
              }
            })
          } else if (userInfo.doctor_status == 2) {
            return wx.showToast({
              title: '信息驳回',
              icon: 'none',
              success: function () {
                wx.reLaunch({
                  url: '/pages/serverRelName/serverRelName',
                })
              }
            })
          } else if (userInfo.doctor_status == 3) {
            return wx.showToast({
              title: '该用户已禁用',
              icon: 'none',
              success: function () {
                wx.removeStorageSync("token")
                wx.removeStorageSync("userInfo")
              }
            })
          }
          wx.reLaunch({
            url: '/pages/serverPages/index/index',
          })
        }else{
          wx.reLaunch({
            url: '/pages/userPages/index/index'
          })
        }        
      }      
    } else {
      wx.reLaunch({
        url: '/pages/login/login'
      })
    }
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      },
      fail: res => {
        wx.openSetting({
          success(res) {
            console.log(res.authSetting)
            // res.authSetting = {
            //   "scope.userInfo": true,
            //   "scope.userLocation": true
            // }
          }
        })
      }
    })
  },
  weeks:{
    '0':"周日",
    '1': "周一",
    '2': "周二",
    '3': "周三",
    '4': "周四",
    '5': "周五",
    '6': "周六",
  },
  getLoginUserInfo(){
    let userInfo = wx.getStorageSync("userInfo")
    if (userInfo) {
      userInfo = JSON.parse(userInfo)
      return userInfo
    }else{
      return null
    }
  },
  globalData: {
    userInfo: null,
    template: template
  },
  url: {
    code: host + "getsmscode.json", //获取二维码
    login: host + "loginByCode.json", //登录
    memberUpdateInfo: host +  "memberUpdateInfo.json", //实名认证
    userInfo: host + "doctorDetail.json", //个人信息
    doctors: host +'doctors.json'  , //列表
    doctorAdd: host +'doctorAdd.json'  , //添加
    doctorUpdate: host +'doctorUpdate.json'  , //修改信息
    doctorQcode: host + 'doctorQcode.json', //二维码信息
    memberUpdateInfo: host +'memberUpdateInfo.json'  , //用户、个人信息修改
    doctorDetail: host +'doctorDetail.json'  , //信息
    doctorGetRules: host +'doctorGetRules.json'  , //获取规则信息
    doctorSetRules: host +'doctorSetRules.json'  , //设置规则信息
    doctorRulesDetail: host +'doctorRulesDetail.json'  , //规则信息详情
    doctorSits: host +'doctorSits.json'  , //坐诊信息
    appointmentAdd: host +'appointmentAdd.json'  , //订座
    isCanSpecial: host + 'isCanSpecial.json', //是否可特需订座
    appointmentAddOrder: host +'appointmentAddOrder.json'  , //订座订单创建信息
    appointmentOrderPay: host +'appointmentOrderPay.json'  , //订座订单支付
    mySits: host +'mySits.json'  , //订座列表
    appointments: host +'appointments.json'  , //订座列表
    appointmentDetail: host +'appointmentDetail.json'  , //订座详情
    appointmentCancle: host +'appointmentCancle.json'  , //取消订座
    appointmentDelete: host + 'appointmentDelete.json', //删除订座
    appointmentMiss: host +'appointmentMiss.json'  , //设置失约
    appointmentSucces: host +'appointmentSucces.json'  , //赴约完成
    memberUsers: host +'memberUsers.json'  , //我的使用人
    memberUserAdd: host +'memberUserAdd.json'  , //添加我的使用人
    memberUserUpdate: host +'memberUserUpdate.json'  , //修改我的使用人
    memberUserDetail: host +'memberUserDetail.json'  , //我的使用人详情
    memberUserDelete: host +'memberUserDelete.json'  , //我的使用人删除
    doctorNotices: host +'doctorNotices.json'  , //通知列表
    doctorNoticesAdd: host +'doctorNoticesAdd.json'  , //通知添加
    doctorNoticesUpdate: host +'doctorNoticesUpdate.json'  , //通知修改
    doctorNoticesDetail: host +'doctorNoticesDetail.json'  , //通知详情
    doctorNoticesDelete: host +'doctorNoticesDelete.json'  , //通知删除
    upload: host+"upload.json", //上传
    userUpdateAvatar: host+"userUpdateAvatar.json", //修改头像
    miniToken: host +"miniToken.json",     //小程序token
    miniGetCode: host +"miniGetCode.json",     //获取code
    miniAuth: host +"miniAuth.json",  //微信手机号授权登录
    loginByPhone:host+"loginByPhone.json",   //微信手机号
    doctorQcode: host +"doctorQcode.json"    //医生的二维码
  },
  reg: {
    phone: /^1[3456789]\d{9}$/, //手机号验证
    email: /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g, //邮箱
    idCard: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ //身份证号
  }
})